/**
 * Description:基于配置文件的表格
 * Copyright: 成都和为科技有限公司 2012 
 * author:zhouxingxing
 * CreateDate: 2015-06-18
 * 
 * 属性 
 *   uri - 报表路径
 *   initLoad - 初始化是否加载报表, 默认为true
 *   printable - 是否支持打印，默认为true
 *   saveable - 是否支持保存，默认为false
 */
function Report() {}

Report.inherit(Control);

/**
 * 初始化方法
 */
Report.prototype.init = function () {
	Report.base.init.call(this);
}

Report.prototype.create = function() {
	this._html = document.createElement("div");
	$(this._html).addClass("report");
	
	//打印
	if (this.getAttribute().printable != "false") {
		var toolBar = document.createElement("div");
		$(this._html).append(toolBar);
		var oPrint = Control.newInstance("Button", 
			{text:"打印", "float":"right", "margin-top":"3px"});
		oPrint.init();
		oPrint.initLoad();
		$(toolBar).append(oPrint.getHTML());
		var othis = this;
		$(oPrint.getHTML()).click(function(){
			othis.print();
		});
	}
	var content = document.createElement("div");
	content.id = "content";
	$(this._html).append(content);
}

Report.prototype.initLoad = function() {
	if (this.getAttribute().uri) {
		this._uri = this.getAttribute().uri;
		if (this.getAttribute().initLoad != "false") {
			this.load(this._uri);
		}
	}
}

Report.prototype.load = function(sURI, oParam) {
	$("#content", this.getHTML()).empty();
	this.addWait();
	var othis = this;
	this.loadReport(sURI, oParam, function(cid){
		othis.displayReport(cid);
		othis.removeWait();
	});
}

Report.prototype.addWait = function() {
	var wait = document.createElement("div");
	$(wait).addClass("wait");
	$(this.getHTML()).append(wait);
}

Report.prototype.removeWait = function() {
	$(".wait", this.getHTML()).remove();
}

/**
 * 服务端加载报表，获得生成的id值
 */
Report.prototype.loadReport = function (sURI, oParam, oCallBack) {
	var sData = "<uri>" + sURI + "</uri>";
	var soap = new HWSOAP("/hwsvr2/re");
	soap.addRequest("quickReport", oParam, sData);
	soap.send(null, true ,function(oResult){
		if (null == oResult) {
			return;
		}
		oCallBack(oResult.resultdata.cid);
	});
}

/**
 * 显示报表
 */
Report.prototype.displayReport = function (cid) {
	var sr = this.queryReport(cid);
	if(!sr) return;
	buildReport2($("#content", this.getHTML())[0], 
		sr.resultdata, cid);
}

Report.prototype.queryReport = function (cid) {
	sData = "<cid>"+ cid +"</cid>";
	sData += "<readonly>"+ false +"</readonly>";	
	
	var soap = new HWSOAP("/hwsvr2/re");
	soap.addRequest("viewReport", null, sData);
	soap.send();
	sr = soap.getResult(0);
	if (null != sr) {
		return sr;
	} else {
		return;
	}
}

/**
 * 打印报表
 * @return {Boolean}
 */
Report.prototype.print = function() {
	var headstr = "<html><head><title></title></head><body>";
	var footstr = "</body>";
	var printCnt = $("#content", this.getHTML()).clone(true);
	var printBody = document.createElement("body");
	$(printBody).append(printCnt);
	var body = $("body")[0];
	$(body).detach();
	$("html").append(printBody);
	window.print(); 
	$(printBody).detach();
	$("html").append(body);
	return false;
}
